public class FindPeakElement {
    // 寻找峰值
    // https://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76?tpId=295&tqId=2227748&sourceUrl=%2Fexam%2Foj
    public int findPeakElement (int[] nums) {
        int left =  0;
        int right = nums.length-1;
        if (nums.length == 1) {
            return 0;
        }
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if ((mid == 0 && nums[mid] > nums[mid+1])
                    || (mid == nums.length-1 && nums[mid] > nums[mid-1])
                    || (mid-1 >= 0 && mid+1 < nums.length && nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1])) {
                return mid;
            } else if (mid == 0 || nums[mid] > nums[mid-1]) {
                left = mid + 1;
            } else if (mid == nums.length-1 || nums[mid] < nums[mid-1]) {
                right = mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        FindPeakElement f = new FindPeakElement();
//        System.out.println(f.findPeakElement(new int[]{2,4,1,2,7,8,4}));
        System.out.println(f.findPeakElement(new int[]{3,6}));
    }
}
